package com.uikoo9.manage.y.util;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: shoko (fbjia.com)
 * Date: 13-11-13
 * Time: 下午3:58
 * To change this template use File | Settings | File Templates.
 */
public class RecordUtil {

    static Map<String,String> _coltext = new HashMap<String, String>();
    static {
        _coltext.put("name","名称");
        _coltext.put("content","内容");
        _coltext.put("type","类型");
        _coltext.put("email","邮箱");
        _coltext.put("level","级别");
        _coltext.put("website","网站");
        _coltext.put("remark","备注");
        _coltext.put("pass","密码");
        _coltext.put("order","顺序");
        _coltext.put("click","点击");
    }
    static String[] col = new String[]{"id","createtime","updatetime","inputid"};
    static String[] colnoid = new String[]{"createtime","updatetime","inputid"};
    public static Map<String,String> recordToOnlyForm(java.util.List<Record> list,String Field){
        java.util.List<String> filter = java.util.Arrays.asList(col);
        Map<String,String> _result = new HashMap<String, String>();
        for(Record c: list){
            String _fid = c.get(Field);
            if(!filter.contains(_fid)){
                String _txt = _fid;
                for(String key: _coltext.keySet()){
                    if(_fid.contains(key)){
                        _txt = _coltext.get(key);
                    }
                }
                _result.put(_fid,_txt);
            }
        }
        return _result;
    }

    public static java.util.List<Map> columnToOnlyForm(Map<String,Object> map){
        java.util.List<String> filter = java.util.Arrays.asList(colnoid);
        java.util.List<Map> _result = new java.util.ArrayList<Map>();
        for(String c: map.keySet()){
            Object _fid = map.get(c);
            if(!filter.contains(c)){
                Map _map = new HashMap();
                _map.put("key",c);
                _map.put("value",_fid);
                String _txt = c;
                for(String key: _coltext.keySet()){
                    if(c.contains(key)){
                        _txt =_coltext.get(key);
                    }
                }
                _map.put("lable",_txt);
                _result.add(_map);
            }
        }
        return _result;
    }

    public static Object[] JsontoArray(List l,String fieds){
        Object[] re = new Object[l.size()];
        int i=0;
        for(Object o:l){
            Object oo = new Object();
            if(o instanceof Map){
                oo = ((Map)o).get(fieds);
            }else if(o instanceof Model) {
                oo = ((Model)o).get(fieds);
            }else if(o instanceof Record){
                oo = ((Record)o).get(fieds);
            }
            re[i++] = oo;
        }
        return re;
    }

    public static Object[] JsontoArrayFilter(List l,String fieds,String filter,Object val){
        Object[] re = new Object[l.size()];
        int i=0;
        for(Object o:l){
            Object oo = new Object();
            Object foo = new Object();
            if(o instanceof Map){
                oo = ((Map)o).get(fieds);
                foo = ((Map)o).get(filter);
            }else if(o instanceof Model) {
                oo = ((Model)o).get(fieds);
                foo = ((Model)o).get(filter);
            }else if(o instanceof Record){
                oo = ((Record)o).get(fieds);
                foo = ((Record)o).get(filter);
            }
            if(val !=null && val.equals(foo)){
                re[i++] = oo;
            }
        }
        return re;
    }

    public static List JsontoList(List l,String fieds){
        List re = new ArrayList();
        int i=0;
        for(Object o:l){
            Object oo = new Object();
            if(o instanceof Map){
                oo = ((Map)o).get(fieds);
            }else if(o instanceof Model) {
                oo = ((Model)o).get(fieds);
            }else if(o instanceof Record){
                oo = ((Record)o).get(fieds);
            }
            re.add(oo);
        }
        return re;
    }
    public static List JsontoListFilter(List l,String fieds,String filter,Object val){
        List re = new ArrayList();
        int i=0;
        for(Object o:l){
            Object oo = new Object();
            Object foo = new Object();
            if(o instanceof Map){
                oo = ((Map)o).get(fieds);
                foo = ((Map)o).get(filter);
            }else if(o instanceof Model) {
                oo = ((Model)o).get(fieds);
                foo = ((Model)o).get(filter);
            }else if(o instanceof Record){
                oo = ((Record)o).get(fieds);
                foo = ((Record)o).get(filter);
            }
            if(val !=null && val.equals(foo)){
                re.add(oo);
            }
        }
        return re;
    }

}
